package com.hotel.backstage.action;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hotel.jabc.DB;
	
public class RegAndLoginServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	public void init(ServletConfig conf)throws ServletException{
		super.init(conf);
	}
	@Override
	public void doGet(HttpServletRequest req,HttpServletResponse res)
	throws ServletException,IOException
	{
		doPost(req,res);
	}
	@Override
	public void doPost(HttpServletRequest req,HttpServletResponse res)
	throws ServletException,IOException
	{
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html");
		//得到请求的响应action
		String action = req.getParameter("action");
		//得到session对象
		HttpSession session = req.getSession(true);
		PrintWriter pw = res.getWriter();
		String msg="";
		if(action.equals("reg"))
		{
			//得到注册用户填写的资料
			String uname = req.getParameter("uname").trim();
			String pwd = req.getParameter("pwd").trim();
			String telNum = req.getParameter("telNum").trim();
			String realName = req.getParameter("realName").trim();
			String gender = req.getParameter("gender");
			String email = req.getParameter("email").trim();
			//拼装查看用户是否存在的SQL
			String sqla = "select  * from user where uname ='"+uname+"'";
			if(DB.isExist(sqla))
			{
				msg=uname+":用户名已存在，请重试!!<br/><a href=jsp/reg.jsp>重新注册</a>";
				
			}else{
				//插入新用户的sql
				String sql = "insert into user values ('"+uname+"','"+pwd+"'"+",'"
							+telNum+"','"+realName+"','"+gender+"','"+email+"')";
				DB.update(sql);//执行插入数据
				msg=uname+":注册成功！！<br/><a href=jsp/login.jsp>是否登录？</a>";
			}
			req.setAttribute("msg",msg);
			req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
		}else if(action.equals("login")){
			session.removeAttribute("uname"); //从session中移除当前登录的用户
			String uname = req.getParameter("uname").trim();
			String pwd = req.getParameter("pwd").trim();
			//拼装查看用户是否存在的sql
			String sqla = "select * from user where uname='"+uname+"'";
			if(DB.isExist(sqla))
			{
				String sql = "select pwd from user where uname='"+uname+"'";
				String password = DB.getInfo(sql).trim(); //从数据库得到密码
				if(pwd.equals(password))
				{
					session.setAttribute("uname",uname); //登录成功
					res.sendRedirect("../jsp/main.jsp");//跳转到主页
				}else{
					msg = uname+"密码不正确！！<br><br>"+
							"<a href=../login.jsp>重新登录</a>";
					req.setAttribute("msg", msg); //将信息发送到信息显示页面
					req.getRequestDispatcher("../info.jsp").forward(req, res);		
				}
				
			}
			else{
				msg = uname +"此用户名不存在！！<br><br>"+
						"<a href=../login.jsp>重新登录</a>";
				req.setAttribute("msg", msg);	//将信息发送到信息显示页面
				req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
			}
			
		}
		else if(action.equals("logout"))
		{
			session.removeAttribute("uname"); //从session中移除登录用户
			msg="欢迎再次光临！！"; //提示消息
			req.setAttribute("msg",msg);//设置并转发提示消息
			req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
			
		}
		else if(action.equals("changeMyInfo"))
		{
			String uname = (String)session.getAttribute("uname");
			//得到修改后的信息
			String telNum = req.getParameter("telNum").trim();
			String realName = req.getParameter("realName").trim();
			String gender = req.getParameter("gender");
			String email = req.getParameter("email").trim();
			//生成更新sql
			String sql = "updata user set telNum='"+telNum+"',realName='"+realName+
					"',gender='"+gender+"',email='"+email+"'where uname='"+uname+"'";
			if(DB.update(sql)==1)
			{
				msg = "修改资料成功!!<br/>";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
			}
					
		}
		else if(action.equals("changePwd"))
		{
			//得到提交的信息
			String uname = (String)session.getAttribute("uname");
			String currentPwd = req.getParameter("currentPwd").trim();
			String newPwd = req.getParameter("newPwd").trim();
			//从数据库拿到当前用户的密码
			String sqla = "select pwd from user where uname='"+uname+"'";
			String pwdFromDB = DB.getInfo(sqla);
			if(currentPwd.equals(pwdFromDB))//比较密码
			{
				String sqlb = "updata user set pwd='"+newPwd+"'where uname='"+
								uname+"'";//更新此用户的密码
				DB.update(sqlb);//执行更新
				msg = "密码修改成功！！ 下一次请用新密码登录。<br>";
			}else{
				msg = "您输入的密码不正确，修改失败！！<br>"+
						"<a href=jsp/changeMyInfo.jsp>返回继续修改？</a>";
			}
			req.setAttribute("msg",msg); //将信息发送到信息显示页面
			req.getRequestDispatcher("../jsp/info.jsp").forward(req, res);
		}
		else if(action.equals("adlogin"))
		{
			session.removeAttribute("adname");
			String adname = req.getParameter("adname").trim();//得到登录用户名
			String pwd = req.getParameter("pwd").trim();//得到登录密码
			//拼装从数据库得到登录管理员密码的sql语句
			String sqla ="select adpwd from adinfo where adname='"+adname+"'";
			String pwdFromDB = DB.getInfo(sqla); //查询得到正确的密码
			if(pwdFromDB!=null&&pwd.equals(pwdFromDB)) //登录成功
			{
				session.setAttribute("adname",adname);	//将登录管理员保存进session
				msg = "登录成功"; //提示登录成功
				
			}else{
				//登录失败提示
				msg = "用户名和密码错误，请重新登录！！<br>"+
						"<a href=jsp/adindex.jsp>重新登录</a>";
			}
			//将信息发送到信息显示页面
			req.setAttribute("msg",msg);
			req.getRequestDispatcher("../jsp/adinfo.jsp").forward(req,res);
		}
		else if(action.equals("adlogout"))//注销
		{
			session.removeAttribute("adname");
			msg = "退出成功"; //提示注销成功
			req.setAttribute("msg",msg);
			req.getRequestDispatcher("../jsp/adinfo.jsp").forward(req, res);
		}
	}
}